﻿using OF.Lib.DataAccess.Entity;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace OF.Lib.DataAccess.Core
{
    public static class SQLConfigHelper
    {
        //const string CACHE_LOCKER_PREFIX = "OF_LIB_DA_CORE_";
        //public static DBConfig LoadConfig()
        //{
        //    LocalMemoryCacheHelper cache = new Core.LocalMemoryCacheHelper();
        //    return cache.GetWithCache(CACHE_LOCKER_PREFIX + "DB.Config", _LoadConfig, int.MaxValue);
        //} 
        static Dictionary<string, SQL> m_SqlDic = new Dictionary<string, SQL>();

        private static object _obj = new object();
        private static object _obj1 = new object();
        private static object _obj2 = new object();

        public static SQL GetSQL(string sqlKey)
        {
            if (m_SqlDic.Count == 0)
            {
                lock (_obj)
                {
                    LoadSQLConfig();
                }
            }
            SQL sql = m_SqlDic[sqlKey.Trim().ToUpper()];
            if(sql==null)
            {
                throw new Exception(string.Format("Don't found sql script for SQLKey \"{0}\"!", sqlKey));
            }
            return sql;
        }



        private static void LoadSQLConfig()
        { 
            Regex regex = new Regex(@"@\w*", RegexOptions.IgnoreCase);

            DBConfig dbConfig = DBConfigHelper.GetDbConfig();
            if (dbConfig != null && dbConfig.SQLFileList != null)
            {
                lock (_obj1)
                {
                    foreach (string file in dbConfig.SQLFileList)
                    {
                        lock (_obj2)
                        {
                            string filePath = Path.Combine(GlobalDefine.ConfigurationPath, file);
                            if (File.Exists(filePath))
                            {
                                SQLConfig sqlConfig = XMLHelper.LoadFromXml<SQLConfig>(filePath);
                                if (sqlConfig.SQLList != null)
                                {
                                    foreach (SQL sql in sqlConfig.SQLList)
                                    {
                                        string sqlKey = sql.SQLKey.Trim().ToUpper();
                                        if (m_SqlDic.ContainsKey(sqlKey))
                                        {
                                            throw new Exception(string.Format("SQLKey \"{0}\" 有重复，请检查你的配置文件！", sql.SQLKey.Trim()));
                                        }
                                        m_SqlDic.Add(sqlKey, sql);
                                         
                                    }
                                }
                            }
                            else
                            {
                                throw new Exception(string.Format("Not found sql file {0}", filePath));
                            }
                        }
                    }
                }
            } 
        }
    }
}
